﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SYcwpb.Helpers;
using System.Windows.Media;
using System.Data;
using System.Collections;
using SYcwpb.ViewModels;

namespace SYcwpb.Access
{
    class CrossingRoadColorConfigAccess
    {
        SqlDataLayer sqlHelper = new SqlDataLayer();

        public bool LoadMilesConfig(ref Color color, ref double maxZB, ref double maxBB, ref double maxYB)
        {
            string sql = "select * from CrossingRoadColorConfig where specify = 'maxMilesZB' or specify = 'maxMilesBB' or specify = 'maxMilesYB'";
            DataTable dt = sqlHelper.SelectDt(sql);
            if (null == dt || dt.Rows.Count != 3)
                return false;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (Convert.ToString(dt.Rows[i]["specify"]) == "maxMilesZB")
                    maxZB = Convert.ToDouble(dt.Rows[i]["maxValue"]);
                else if (Convert.ToString(dt.Rows[i]["specify"]) == "maxMilesBB")
                    maxBB = Convert.ToDouble(dt.Rows[i]["maxValue"]);
                else if (Convert.ToString(dt.Rows[i]["specify"]) == "maxMilesYB")
                    maxYB = Convert.ToDouble(dt.Rows[i]["maxValue"]);
            }

            color = Color.FromArgb(Convert.ToByte(dt.Rows[0]["colorA"]),
                Convert.ToByte(dt.Rows[0]["colorR"]),
                Convert.ToByte(dt.Rows[0]["colorG"]),
                Convert.ToByte(dt.Rows[0]["colorB"])
                );

            return true;
        }

        public bool LoadWhoursConfig(ref Color color, ref double maxZB, ref double maxBB, ref double maxYB)
        {
            string sql = "select * from CrossingRoadColorConfig where specify = 'maxWhoursZB' or specify = 'maxWhoursBB' or specify = 'maxWhoursYB'";
            DataTable dt = sqlHelper.SelectDt(sql);
            if (null == dt || dt.Rows.Count != 3)
                return false;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (Convert.ToString(dt.Rows[i]["specify"]) == "maxWhoursZB")
                    maxZB = Convert.ToDouble(dt.Rows[i]["maxValue"]);
                else if (Convert.ToString(dt.Rows[i]["specify"]) == "maxWhoursBB")
                    maxBB = Convert.ToDouble(dt.Rows[i]["maxValue"]);
                else if (Convert.ToString(dt.Rows[i]["specify"]) == "maxWhoursYB")
                    maxYB = Convert.ToDouble(dt.Rows[i]["maxValue"]);
            }

            color = Color.FromArgb(Convert.ToByte(dt.Rows[0]["colorA"]),
                Convert.ToByte(dt.Rows[0]["colorR"]),
                Convert.ToByte(dt.Rows[0]["colorG"]),
                Convert.ToByte(dt.Rows[0]["colorB"])
                );

            return true;
        }

        public bool SaveConfig(ColorConfig colorConfig,ref string err)
        {
            try
            {
                ArrayList sqlList = new ArrayList();
                string updateMilesZB = "update CrossingRoadColorConfig set maxValue = '" + colorConfig.WarnMilesZB + "', colorA = '" + colorConfig.WarnColorMiles.A + "',"
                    + "colorR = '" + colorConfig.WarnColorMiles.R + "',colorG = '" + colorConfig.WarnColorMiles.G + "', colorB = '" + colorConfig.WarnColorMiles.B + "' where specify = 'maxMilesZB'";

                string updateMilesBB = "update CrossingRoadColorConfig set maxValue = '" + colorConfig.WarnMilesBB + "', colorA = '" + colorConfig.WarnColorMiles.A + "',"
                    + "colorR = '" + colorConfig.WarnColorMiles.R + "',colorG = '" + colorConfig.WarnColorMiles.G + "', colorB = '" + colorConfig.WarnColorMiles.B + "' where specify = 'maxMilesBB'";

                string updateMilesYB = "update CrossingRoadColorConfig set maxValue = '" + colorConfig.WarnMilesYB + "', colorA = '" + colorConfig.WarnColorMiles.A + "',"
                    + "colorR = '" + colorConfig.WarnColorMiles.R + "',colorG = '" + colorConfig.WarnColorMiles.G + "', colorB = '" + colorConfig.WarnColorMiles.B + "' where specify = 'maxMilesYB'";

                string updateWhoursZB = "update CrossingRoadColorConfig set maxValue = '" + colorConfig.WarnWhourZB + "', colorA = '" + colorConfig.WarnColorWhours.A + "',"
                    + "colorR = '" + colorConfig.WarnColorWhours.R + "',colorG = '" + colorConfig.WarnColorWhours.G + "', colorB = '" + colorConfig.WarnColorWhours.B + "' where specify = 'maxWhoursZB'";

                string updateWhoursBB = "update CrossingRoadColorConfig set maxValue = '" + colorConfig.WarnWhourBB + "', colorA = '" + colorConfig.WarnColorWhours.A + "',"
                    + "colorR = '" + colorConfig.WarnColorWhours.R + "',colorG = '" + colorConfig.WarnColorWhours.G + "', colorB = '" + colorConfig.WarnColorWhours.B + "' where specify = 'maxWhoursBB'";

                string updateWhoursYB = "update CrossingRoadColorConfig set maxValue = '" + colorConfig.WarnWhourYB + "', colorA = '" + colorConfig.WarnColorWhours.A + "',"
                    + "colorR = '" + colorConfig.WarnColorWhours.R + "',colorG = '" + colorConfig.WarnColorWhours.G + "', colorB = '" + colorConfig.WarnColorWhours.B + "' where specify = 'maxWhoursYB'";

                sqlList.Add(updateMilesZB);
                sqlList.Add(updateMilesBB);
                sqlList.Add(updateMilesYB);

                sqlList.Add(updateWhoursZB);
                sqlList.Add(updateWhoursBB);
                sqlList.Add(updateWhoursYB);

                sqlHelper.ExecuteSqlTran(sqlList);
            }
            catch (System.Exception ex)
            {
                err = ex.Message;
                return false;
            }
            return true;
        }
    }
}
